package src;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

/**
 * The GUI_Login returns a JPanel with the interface necessary to set up a game. Gets information
 * from human such as what kind of player (human, computer, online), names of player, and player
 * color. 
 * @author Akshat
 */
public class GUI_Login extends JTabbedPane
{
	private SuperTicTacToeController controller; 
	
	private InfoPanel p1; 
	private InfoPanel p2; 	
	
	final static String HUMAN_PLAYER = "Human Player";
	final static String COMPUTER_PLAYER = "Computer Player";
	final static String NETWORKED_PLAYER = "Networked Player";
	
	public GUI_Login(SuperTicTacToeController ctrl)
	{		
		controller = ctrl; 
		this.setOpaque(false);
		
		JPanel local = new JPanel();
		local.setLayout( new GridBagLayout() );
		
		GridBagConstraints c = new GridBagConstraints(); 
		c.insets = new Insets(10, 10, 10, 10); 
		c.fill = GridBagConstraints.BOTH; 
	
		p1 = new InfoPanel(); 
		
		c.gridx = 0; 
		c.gridy = 1;
		c.weighty = 1.0; 
		c.gridwidth = 1;
		
		local.add(p1, c);
		
		p2 = new InfoPanel(); 
		
		c.gridx = 1; 
		c.gridy = 1; 
		c.weighty = 1.0;
		c.gridwidth = 1;
		
		local.add(p2, c);
		
		JPanel online = new JPanel();
		online.add(new JLabel("PRESS OK TO PLAY ONLINE"));

		this.addTab("Local", null, local, "Play on local machine");
		this.addTab("Online", null, online, "Play online");
		
	}
	
	/**
	 * Compiles an array of players based on the data provided by the human. 
	 * @return an array of players that will be playing the game
	 */
	public Player[] getPlayers()
	{
		if( p1.getPlayer() != null && p2.getPlayer() != null )
		{
			Player[] players = new Player[2];
			players[0] = p1.getPlayer(); 
			players[1] = p2.getPlayer(); 
			return players;
			
		}
		else
		{
			return null; 
		}
	}
		
	/**
	 * An interface to collect information about a Human Player
	 * @author Akshat
	 */
	public class HumanPlayerPanel extends JPanel
	{
		private JTextField enterName; 
		private JColorChooser selectColor; 
		
		public HumanPlayerPanel()
		{			
			setLayout( new BoxLayout(this, BoxLayout.PAGE_AXIS));
			
			
			JLabel nameLabel = new JLabel("Name:");
			enterName = new JTextField(12); 
			nameLabel.setLabelFor(enterName);

			JPanel name = new JPanel();
			name.add(nameLabel);
			name.add(enterName);
			
			add(name);
			
			selectColor = new JColorChooser();
			selectColor.setPreviewPanel(new JPanel());
			add(selectColor);
		}
		
		
		/** 
		 * Getter method for name
		 *@return name
		 */
		public String getName()
		{
			return enterName.getText();
		}
		
		/**
		 * Getter method for color
		 * @return color
		 */
		public Color getColor()
		{
			return selectColor.getColor();
		}
		
		/**
		 * Creates and returns a human player based on input
		 * @return Player
		 */
		public Player getPlayer()
		{
			if(enterName.getText().length() != 0 && selectColor.getColor() != null)
			{
				return new HumanPlayer(enterName.getText(), selectColor.getColor()); 
			}
			else
			{
				return null; 
			}
		}
	}
	
	/**
	 * An interface for compiling information about a computer player
	 * @author Akshat
	 */
	public class ComputerPlayerPanel extends JPanel
	{
		private JColorChooser selectColor; 
		
		public ComputerPlayerPanel()
		{
			
			selectColor = new JColorChooser();
			selectColor.setPreviewPanel(new JPanel());
			add(selectColor, BorderLayout.CENTER);
		}
		
		/**
		 * Getter method for color
		 * @return color
		 */
		public Color getColor()
		{
			return selectColor.getColor(); 
		}
		
		/**
		 * Creates and returns a computer player based on input
		 * @return player
		 */
		public Player getPlayer()
		{
			if(selectColor.getColor() != null)
			{
				return new ComputerPlayer(selectColor.getColor()); 
			}
			else
			{
				return null; 
			}
		}
	}
	
//	public class NetworkedPlayerPanel extends JPanel
//	{
//		private JTextField enterIP; 
//		private JTextField enterPort; 
//		private JTextField enterNickname; 
//		
//		public NetworkedPlayerPanel()
//		{
//			//setLayout( new BoxLayout(this, BoxLayout.PAGE_AXIS));
//			setLayout( new GridBagLayout() );
//			GridBagConstraints c = new GridBagConstraints(); 	
//			c.fill = GridBagConstraints.HORIZONTAL; 
//			
//			JLabel ip = new JLabel("Enter IP:", SwingConstants.RIGHT);
//			JLabel port = new JLabel("Port number:", SwingConstants.RIGHT);
//			JLabel nickname = new JLabel("Enter nickname:", SwingConstants.RIGHT);
//			
//			ip.setLabelFor(enterIP);
//			ip.setHorizontalAlignment(JLabel.RIGHT);
//			ip.setAlignmentX(Component.RIGHT_ALIGNMENT);
//			ip.setBackground(Color.BLUE);
//			
//			port.setLabelFor(enterPort);
//			port.setHorizontalAlignment(JLabel.RIGHT);
//			port.setAlignmentX(Component.RIGHT_ALIGNMENT);
//
//			nickname.setLabelFor(enterNickname);
//			nickname.setHorizontalAlignment(JLabel.RIGHT);
//			nickname.setAlignmentX(Component.RIGHT_ALIGNMENT);
//
//			enterIP = new JTextField(12); 
//			enterIP.setMaximumSize(enterIP.getPreferredSize());
//			
//			enterPort = new JTextField(12);
//			enterPort.setMaximumSize(enterPort.getPreferredSize());
//			
//			enterNickname = new JTextField(12); 
//			enterNickname.setMaximumSize(enterNickname.getPreferredSize());
//			
//			c.gridx = 0; 
//			c.gridy = 0; 
//			
//			add(ip, c);
//			
//			c.gridx = 1; 
//			c.gridy = 0; 
//			
//			add(enterIP, c);
//			
//			c.gridx = 0; 
//			c.gridy = 1; 
//			
//			add(port, c);
//			
//			c.gridx = 1; 
//			c.gridy = 1; 
//			
//			add(enterPort, c);
//			
//			c.gridx = 0; 
//			c.gridy = 2; 
//			
//			add(nickname, c);
//			
//			c.gridx = 1; 
//			c.gridy = 2; 
//			
//			add(enterNickname, c);
//			
//			connect = new JButton("Connect");
//			//connect.addActionListener(this);
//			
//			c.gridx = 0; 
//			c.gridy = 3;
//			c.gridwidth = 2; 
//			
//			add(connect, c);
//			
//		}
//		
//		public Player getPlayer()
//		{
//			if(enterIP.getText().length() != 0 && enterPort.getText().length() != 0 && enterNickname.getText().length() != 0)
//			{
//				//TODO: NETWORKING
//				return new ComputerPlayer(Color.BLUE);
//			}
//			else
//			{
//				return null; 
//			}
//		}
//		
//		public JTextField getIP()
//		{
//			return enterIP;
//		}
//		
//		public JTextField getPort()
//		{
//			return enterPort;
//		}
//		
//		public JTextField getNickname()
//		{
//			return enterNickname; 
//		}
//		
//	}
	
	/**
	 * A overarching class that creates instances of humanplayerpanel and computerplayerpanel.
	 * Two instances are created in the parent class GUI_Login. 
	 * @author Akshat
	 */
	public class InfoPanel extends JPanel implements ItemListener, ActionListener
	{
		//private JComboBox playerOptions;
		private JRadioButton human;
		private JRadioButton comp;
		private JRadioButton network;
		private JPanel playerInfo;
		
		private HumanPlayerPanel humanPlayerPanel;
		private ComputerPlayerPanel computerPlayerPanel;
		
		public InfoPanel()
		{
			setLayout( new BoxLayout(this, BoxLayout.PAGE_AXIS) );

			//Sub-panel that contains element for picking type of player
			JPanel selectPlayerType = new JPanel(); 
			ButtonGroup rbuttons = new ButtonGroup(); 
			
			human = new JRadioButton("Human");
			comp = new JRadioButton("Computer");
			
			human.addActionListener(this);
			comp.addActionListener(this);
			
			rbuttons.add(human);
			rbuttons.add(comp);
			human.setSelected(true);
			
			selectPlayerType.add(human);
			selectPlayerType.add(comp);
			
//			String[] playerTypes = {
//					HUMAN_PLAYER,
//					COMPUTER_PLAYER,
//					NETWORKED_PLAYER
//			};
//			
//			playerOptions = new JComboBox(playerTypes);
//			playerOptions.setEditable(false);
//			playerOptions.addItemListener(this);
//			
//			selectPlayerType.add(playerOptions);
//			
			playerInfo = new JPanel( new CardLayout() );
			
			humanPlayerPanel = new HumanPlayerPanel();
			computerPlayerPanel = new ComputerPlayerPanel();
			
			playerInfo.add(humanPlayerPanel, HUMAN_PLAYER);
			playerInfo.add(computerPlayerPanel, COMPUTER_PLAYER);
			
			add(selectPlayerType, BorderLayout.PAGE_START);
			add(playerInfo, BorderLayout.CENTER);
		}
		
		public Player getPlayer()
		{
//			if(playerOptions.getSelectedItem().equals(HUMAN_PLAYER))
//			{
//				return humanPlayerPanel.getPlayer(); 
//			}
//			else if(playerOptions.getSelectedItem().equals(COMPUTER_PLAYER))
//			{
//				return computerPlayerPanel.getPlayer();
//			}
//			else
//			{
//				return networkedPlayerPanel.getPlayer();
//			}
			
			if(human.isSelected())
			{
				return humanPlayerPanel.getPlayer();
			}
			else
			{
				return computerPlayerPanel.getPlayer(); 
			}

		}

		@Override
		public void itemStateChanged(ItemEvent e) 
		{
			CardLayout c = (CardLayout)playerInfo.getLayout();
			c.show(playerInfo, (String)e.getItem());
		}

		@Override
		public void actionPerformed(ActionEvent e) 
		{
			CardLayout c = (CardLayout)playerInfo.getLayout();
			JRadioButton b = (JRadioButton)e.getSource(); 
			
			if( b.getText().equals("Human"))
			{
				c.show(playerInfo, HUMAN_PLAYER);
			}
			else if( b.getText().equals("Computer"))
			{
				c.show(playerInfo, COMPUTER_PLAYER);
			}
			else if( b.getText().equals("Network"))
			{
				c.show(playerInfo, NETWORKED_PLAYER); 
			}
			
		}
		
	}

	
}
